package com.zhilianbao.leyaogo.ui.fragment.me.myorder;

import com.bql.utils.EventManager;
import com.zhilianbao.leyaogo.events.LeEventCode;
import com.zhilianbao.leyaogo.model.response.me.MyOrderResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liu on 2016/10/25.
 * 查看全部订单
 */
public class AllFragment extends BaseOrderFragment {

    private int mOrderType;

    public static AllFragment getInstance() {
        AllFragment fragment = new AllFragment();
        return fragment;
    }

    @Override
    public boolean registerEventBus() {
        return true;
    }

    @Override
    protected void onHandleEvent(EventManager eventManager) {
        super.onHandleEvent(eventManager);
        switch (eventManager.getEventCode()) {
            case LeEventCode.MSG_CODE_ORDER_CANCEL:
            case LeEventCode.MSG_CODE_SURE_RECEIVE:
                mOrderType = (int) eventManager.getData();
                if (mMyOrderAdapter != null && mOrderType != 0) {
                    loadDataList(1, false);
                }
                break;
            case LeEventCode.MSG_CODE_ORDER_ITEM_DELETE:
                mOrderType = (int) eventManager.getData();
                if (mMyOrderAdapter != null && mOrderType != 0) {
                    loadDataList(1, false);
                } else if (mMyOrderAdapter != null && mOrderType == 0) {
                    if (mList.size() == 0) {
                        loadDataList(1, false);
                    }
                }
                break;
            case LeEventCode.MSG_CODE_ORDER_DETAIL_CANCEL://订单详情取消订单
            case LeEventCode.MSG_CODE_ALI_PAY_SUCCESS://支付宝支付成功
            case LeEventCode.MSG_CODE_PAY_SUCCESS://微信支付成功
            case LeEventCode.MSG_CODE_YUE_PAY_SUCCESS://余额支付成功
            case LeEventCode.MSG_CODE_ORDER_DETAIL_SURE_RECEIVE://订单详情确认收货
                if (mMyOrderAdapter != null) {
                    loadDataList(1, false);
                }
                break;
            case LeEventCode.MSG_CODE_ORDER_DETAIL_DELETE://订单详情删除订单
                int position = (int) eventManager.getData();
                if (mMyOrderAdapter != null) {
                    mList.remove(position);
                    mMyOrderAdapter.notifyItemRemoved(position);
                    mMyOrderAdapter.notifyItemRangeChanged(position, mList.size());
                }
                break;

        }

    }

    @Override
    public int getType() {
        return 0;
    }

    @Override
    public void initCurrentChild() {
        //0 all,1 waitToPay,2 waitToSend,3 sending,4 finish
        currentChild = 0;
    }

    @Override
    public List<MyOrderResponse> getList(List<MyOrderResponse> myOrderResponses) {
        List<MyOrderResponse> list = new ArrayList<>();
        if (myOrderResponses == null)
            return null;
        for (int i = 0; i < myOrderResponses.size(); i++) {
            MyOrderResponse myOrderResponse = myOrderResponses.get(i);
            list.add(myOrderResponse);
        }

        return list;
    }

}
